package cn.tta.service.impl;

import cn.tta.common.constant.MessageConstant;
import cn.tta.common.constant.StatusConstant;
import cn.tta.common.exception.DeletionNotAllowedException;
import cn.tta.common.result.PageResult;
import cn.tta.mapper.DishFlaverMapper;
import cn.tta.mapper.DishMapper;
import cn.tta.mapper.SetmealDishMapper;
import cn.tta.pojo.dto.DishDTO;
import cn.tta.pojo.dto.DishPageQueryDTO;
import cn.tta.pojo.entity.Dish;
import cn.tta.pojo.entity.DishFlavor;
import cn.tta.pojo.vo.DishVO;
import cn.tta.service.DishService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlaverMapper dishFlaverMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);

        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0 ) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlaverMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishFlaverMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {

        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                // 当前菜品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> stemealId = setmealDishMapper.getSetmealIdsByDishId(ids);
        if (stemealId != null && stemealId.size() > 0 ) {
            // 当前菜品被套餐关联了，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlaverMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> dishFlavors = dishFlaverMapper.getByDishId(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlaverMapper.deleteByDishId(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0 ) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
        }
        dishFlaverMapper.insertBatch(flavors);
    }

    @Override
    public void startOrStop(Integer status,Long id) {
        Dish build = Dish.builder().status(status).id(id).build();
        dishMapper.update(build);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish build = Dish.builder().categoryId(categoryId).status(StatusConstant.ENABLE).build();
        return dishMapper.list(build);
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> list = dishMapper.list(dish);

        ArrayList<DishVO> dishVOS = new ArrayList<>();
        for (Dish d : list) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            List<DishFlavor> byDishId = dishFlaverMapper.getByDishId(d.getId());
            dishVO.setFlavors(byDishId);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
}
